package com.example.springboot004extensionpoint.bootstrapRegistryInitializer;

import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.BootstrapRegistryInitializer;

public class KoalaCustomBootstrapRegistryInitializer implements BootstrapRegistryInitializer {
    @Override
    public void initialize(BootstrapRegistry registry) {
        System.out.println("KoalaCustom *** BootstrapRegistryInitializer");

        //添加一个 ApplicationListener，当 BootstrapContext 关闭时，它将使用 BootstrapContextClosedEvent 调用，并且 ApplicationContext 已准备好。
        registry.addCloseListener((event) -> {
            User user = new User("我是应用上下文加载后才能注册的bean", 18);
            event.getApplicationContext().getBeanFactory().registerSingleton("myUser", user);
        });
        User user2 = new User("我是系统未启动就可以加载的", 20);
        registry.registerIfAbsent(User.class, BootstrapRegistry.InstanceSupplier.of(user2));
    }
}
